﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Linq.Expressions;
using System.Collections.Generic;
using T2V.SharePoint.Common.Utilities.Camlex;
using T2V.SharePoint.Common.Utilities;
using PepsiCo.Common;

namespace PepsiCo.Intranet.Receivers.MegaMenuItemER
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class MegaMenuItemER : SPItemEventReceiver
    {
       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);

           IncreaseItemOrderNo(properties);

           TrimTextFieldValue(properties, PepsiCoConstants.MegaMenuItem.TITLE_COLUMN);
           TrimTextFieldValue(properties, PepsiCoConstants.MegaMenuItem.HYPERLINK_COLUMN);
           TrimTextFieldValue(properties, PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN);
       }

       /// <summary>
       /// An item is being updated.
       /// </summary>
       public override void ItemUpdating(SPItemEventProperties properties)
       {
           base.ItemUpdating(properties);

           IncreaseItemOrderNo(properties);

           TrimTextFieldValue(properties, PepsiCoConstants.MegaMenuItem.TITLE_COLUMN);
           TrimTextFieldValue(properties, PepsiCoConstants.MegaMenuItem.HYPERLINK_COLUMN);
           TrimTextFieldValue(properties, PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN);
       }

       /// <summary>
       /// An item is being deleted.
       /// </summary>
       public override void ItemDeleting(SPItemEventProperties properties)
       {
           base.ItemDeleting(properties);
       }

       private void IncreaseItemOrderNo(SPItemEventProperties properties)
       {
           if (properties.AfterProperties[PepsiCoConstants.MegaMenuItem.ITEM_ORDER_COLUMN] != null && !string.IsNullOrEmpty(properties.AfterProperties[PepsiCoConstants.MegaMenuItem.ITEM_ORDER_COLUMN].ToString()))
           {
               int orderNo = 0;
               int.TryParse(properties.AfterProperties[PepsiCoConstants.MegaMenuItem.ITEM_ORDER_COLUMN].ToString(), out orderNo);

               if (orderNo != 0)
               {
                   string strItemId = properties.ListItem == null ? "0" : properties.ListItemId.ToString();

                   string caml = string.Empty;
                   var expressionsAnd = new List<Expression<Func<SPListItem, bool>>>();

                   expressionsAnd.Add(x => ((int)x[PepsiCoConstants.MegaMenuItem.ITEM_ORDER_COLUMN]) >= orderNo);
                   expressionsAnd.Add(x => (x["ID"]) != (DataTypes.Counter)strItemId);

                   if (properties.AfterProperties[PepsiCoConstants.MegaMenuItem.SECTION_COLUMN] != null)
                   {
                       expressionsAnd.Add(x => (x[PepsiCoConstants.MegaMenuItem.SECTION_COLUMN]) == (DataTypes.LookupId)properties.AfterProperties[PepsiCoConstants.MegaMenuItem.SECTION_COLUMN].ToString());
                   }
                   if (properties.AfterProperties[PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN] != null && !string.IsNullOrEmpty(properties.AfterProperties[PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN].ToString().Trim()))
                   {
                       expressionsAnd.Add(x => ((string)x[PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN]) == properties.AfterProperties[PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN].ToString().Trim());
                   }
                   else
                   {
                       expressionsAnd.Add(x => (x[PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN]) == null);
                   }

                   caml = Camlex.Query().WhereAll(expressionsAnd).OrderBy(x => x[PepsiCoConstants.MegaMenuItem.ITEM_ORDER_COLUMN] as Camlex.Asc).ToString();

                   SPQuery spQuery = new SPQuery();
                   spQuery.Query = caml;

                   SPList currentList = properties.List;
                   SPListItemCollection items = currentList.GetItems(spQuery);

                   foreach (SPListItem item in items)
                   {
                       orderNo++;

                       using (DisableItemEvent scope = new DisableItemEvent())
                       {
                           item[PepsiCoConstants.MegaMenuItem.ITEM_ORDER_COLUMN] = orderNo;
                           item.SystemUpdate(false);
                       }
                   }
               }
           }
           else
           {
               properties.AfterProperties[PepsiCoConstants.MegaMenuItem.ITEM_ORDER_COLUMN] = GetLatestItemOrderNo(properties) + 1;
           }
       }

       private int GetLatestItemOrderNo(SPItemEventProperties properties)
       {
           int orderNo = 0;

           string caml = string.Empty;
           var expressionsAnd = new List<Expression<Func<SPListItem, bool>>>();

           if (properties.AfterProperties[PepsiCoConstants.MegaMenuItem.SECTION_COLUMN] != null)
           {
               expressionsAnd.Add(x => (x[PepsiCoConstants.MegaMenuItem.SECTION_COLUMN]) == (DataTypes.LookupId)properties.AfterProperties[PepsiCoConstants.MegaMenuItem.SECTION_COLUMN].ToString());
           }
           if (properties.AfterProperties[PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN] != null && !string.IsNullOrEmpty(properties.AfterProperties[PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN].ToString().Trim()))
           {
               expressionsAnd.Add(x => ((string)x[PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN]) == properties.AfterProperties[PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN].ToString().Trim());
           }
           else
           {
               expressionsAnd.Add(x => (x[PepsiCoConstants.MegaMenuItem.SUBSECTION_COLUMN]) == null);
           }

           if (expressionsAnd.Count > 0)
               caml = Camlex.Query().WhereAll(expressionsAnd).OrderBy(x => x[PepsiCoConstants.MegaMenuItem.ITEM_ORDER_COLUMN] as Camlex.Desc).ToString();
           else
               caml = Camlex.Query().OrderBy(x => x[PepsiCoConstants.MegaMenuItem.ITEM_ORDER_COLUMN] as Camlex.Desc).ToString();

           SPQuery spQuery = new SPQuery();
           spQuery.Query = caml;

           spQuery.RowLimit = 1;

           SPList currentList = properties.List;
           SPListItemCollection items = currentList.GetItems(spQuery);

           if (items != null && items.Count > 0)
           {
               SPListItem item = items[0];

               if (item != null)
               {
                   orderNo = Convert.ToInt32(item[PepsiCoConstants.MegaMenuItem.ITEM_ORDER_COLUMN]);
               }
           }

           return orderNo;
       }

       private void TrimTextFieldValue(SPItemEventProperties properties, string fieldName)
       {
           if (properties.AfterProperties[fieldName] != null)
           {
               properties.AfterProperties[fieldName] = properties.AfterProperties[fieldName].ToString().Trim();
           }
           else
           {
               properties.AfterProperties[fieldName] = string.Empty;
           }
       }
    }
}
